import urllib.request
import pandas as pd

bull_data = []


def url_pachong():
    global test, bull_data
    blue_list = [89, 122, 153, 154, 153, 154, 154, 153, 154, 154, 154, 152, 154, 153, 154, 153, 151, 134, 24]
    for i in range(19):
        for j in range(blue_list[i]):
            attempts = 0
            success = False
            data = []
            while attempts < 3 and success == False:
                try:
                    if i < 7:
                        response = urllib.request.urlopen(
                            "https://kaijiang.500.com/shtml/ssq/0" + str((i + 3) * 1000 + 1 + j) + ".shtml")
                        print(str((i + 3) * 1000 + 1 + j))
                    else:
                        response = urllib.request.urlopen(
                            "https://kaijiang.500.com/shtml/ssq/" + str((i + 3) * 1000 + 1 + j) + ".shtml")
                        print(str((i + 3) * 1000 + 1 + j))
                    if (i + 3) * 1000 + 1 + j == 17001:
                        success = True
                        continue
                    html = response.read().decode("gbk", "ignore")
                    a = 0
                    for k in range(6):
                        html_data = html.find('<li class="ball_red">', a + 10)
                        a = html_data
                        data.append(int(html[html_data + 21] + html[html_data + 22]))
                    html_data = html.find('<li class="ball_blue">')
                    data.append(int(html[html_data + 22] + html[html_data + 23]))
                    bull_data.append(list(data))
                    success = True
                except:
                    attempts += 1
                    print('error')
                    if attempts == 3:
                        break
        print(bull_data)
        print("阶段%d完成" % int(i + 3))


if __name__ == "__main__":
    url_pachong()
    test = pd.DataFrame(columns=['red1', 'red2', 'red3', 'red4', 'red5', 'red6', 'blue'], data=bull_data)
    test.to_csv('./bull_data2_c.csv')

